<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

class CI_Discount_ticket_entity{
	var $current_dbprefix = "";
	var $discount_code = '';
	var $discount_type_code = 0;
	var $description = '';
	var $used_time = 0;
	var $created_time = 0;
	var $active = 0;
	
	function __construct() {
		$ci= & get_instance();
		$this->current_dbprefix = $ci->db->dbprefix;
		$ci->db->set_dbprefix('default_');
	}
	function __destruct() {
		$ci= & get_instance();
		$ci->db->set_dbprefix($this->current_dbprefix);
	}
	
	function setDiscountCode($input){
		$this->discount_code=$input;
	}
	function setDiscountTypeCode($input){
		$this->discount_type_code = $input;
	}
	function setDescription($input){
		$this->description = $input;
	}
	function setUsedTime($input){
		$this->used_time = $input;
	}
	function setCreatedTime($input){
		$this->created_time = $input;
	}
	function setStatus($input){
		$this->active = $input;
	}
	function getDiscountCode(){
		return $this->discount_code;
	}
	function getDiscountTypeCode(){
		return $this->discount_type_code;
	}
	function getDescription(){
		return $this->description;
	}
	function getUsedTime(){
		return $this->used_time;
	}
	function getCreatedTime(){
		return $this->created_time;
	}
	function getActive(){
		return $this->active;
	}
	
	function init($cmd, $input)
	{
		$ci=& get_instance();
		$ci->db->set_dbprefix('default_');
		
		if($cmd == 'discount_code')
		{
			$result = $ci->db->where('discount_code',$input)->get(TBL_DISCOUNT_TICKET)->result();
		}else{
			return false;
		}
		if( !empty($result) )
		{
			$this->discount_code = $result[0]->discount_code;
			$this->discount_type_code = $result[0]->discount_type_code;
			$this->description = $result[0]->description;
			$this->used_time = $result[0]->used_time;
			$this->created_time = $result[0]->created_time;
			$this->active = $result[0]->active;
			return $this;
		}
		return false;
	}
	
	function getList($limit = array()){
		$ci=& get_instance();
		$ci->db->set_dbprefix('default_');
		
		if(count($limit) >0){
			$result = $ci->db->limit($limit[0], $limit[1]);
		}
		$ci->db->order_by('created_time', 'DESC');
		return $ci->db->get(TBL_DISCOUNT_TICKET)->result();
	}
	function count(){
		$ci=& get_instance();
		$ci->db->set_dbprefix('default_');
		
		return $ci->db->count_all_results(TBL_DISCOUNT_TICKET);
	}
	
	function save(){
		$ci=& get_instance();
		$ci->db->set_dbprefix('default_');
		
		$ci->db->set('discount_code',$this->getAmount());
		$ci->db->set('discount_type_code',$this->getUnit());
		$ci->db->set('description',$this->getDescription());
		$ci->db->set('used_time',$this->getUsedTime());
		$ci->db->set('created_time',$this->getCreatedTime());
		$ci->db->set('active',$this->getActive());
		$ci->db->insert(TBL_DISCOUNT_TICKET);
		return $ci->db->insert_id();
	}
	function update(){
		$ci=& get_instance();
		$ci->db->set_dbprefix('default_');
		
		$ci->db->set('discount_code',$this->getAmount());
		$ci->db->set('discount_type_code',$this->getUnit());
		$ci->db->set('description',$this->getDescription());
		$ci->db->set('used_time',$this->getUsedTime());
		$ci->db->set('created_time',$this->getCreatedTime());
		$ci->db->set('active',$this->getActive());
		return $ci->db->where('id', $this->id)->update(TBL_DISCOUNT_TICKET);
	}
	function delete($id){
		$ci=& get_instance();
		$ci->db->set_dbprefix('default_');
		
		$ci->db->where('id',$id);
		$ci->db->delete(TBL_DISCOUNT_TICKET);
	}
	
	function getDiscountEscortByPromotionCode($code,$escort_id){
		$ci=& get_instance();
		$ci->db->set_dbprefix('default_');
		
		if(!$code) return false;
		
		$res = $ci->db->
					query("SELECT A.*,B.* FROM ".TBL_DISCOUNT_TICKET." AS A INNER JOIN ".TBL_DISCOUNT_TYPE." AS B 
							ON A.discount_type_code=B.id WHERE A.active=1 AND A.discount_code='$code'")
					->result();
		$res2 = $ci->db->where( array('promotion_code'=>$code, 'escort_id'=>$escort_id) )->get(TBL_PAYMENT_HISTORY)->result();	
		if($res2) return false;
		
		return ($res) ? $res[0]:false;			
	}
	
	function getDiscountByPromotionCode($code){
		$ci=& get_instance();
		$ci->db->set_dbprefix('default_');
		
		if(!$code) return false;
		
		$res = $ci->db->
					query("SELECT A.*,B.* FROM ".TBL_DISCOUNT_TICKET." AS A INNER JOIN ".TBL_DISCOUNT_TYPE." AS B 
							ON A.discount_type_code=B.id WHERE A.active=1 AND A.discount_code='$code'")
					->result();
		return ($res) ? $res[0]:false;					
	}
	
	public function countActiveDiscount(){
		$ci=& get_instance();
		$ci->db->set_dbprefix('default_');
		
		$ci->db->where('active', 1);
		return $ci->db->count_all_results(TBL_DISCOUNT_TICKET);
	}
	
	//end class
}